package by.amakarevich.services.impl;

import javax.inject.Inject;

import org.apache.commons.lang3.Validate;
import org.springframework.stereotype.Service;

import by.amakarevich.dataaccess.ShoppingCartDao;
import by.amakarevich.dataaccess.UserAccountDao;
import by.amakarevich.datamodel.ShoppingCart;
import by.amakarevich.datamodel.UserAccount;
import by.amakarevich.services.ShoppingCartService;



@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
	
	@Inject
	private ShoppingCartDao shoppingCartDao;

	@Inject
	private UserAccountDao userAccountDao;

	@Override
	public void createNewShoppingCart(ShoppingCart shoppingCart, UserAccount userAccount) {
		Validate.isTrue(
				shoppingCart.getId() == null,
				"This method should be called for 'not saved yet' profile only. Use UPDATE instead");
		shoppingCart.setUserAccount(userAccount);
		shoppingCartDao.insert(shoppingCart);
	}

	@Override
	public ShoppingCart get(Long id) {
		return shoppingCartDao.getById(id);
	}

	@Override
	public void updateShoppingCart(ShoppingCart shoppingCart) {
		shoppingCartDao.update(shoppingCart);
	}

	@Override
	public void removeShoppingCart(Long id) {

		shoppingCartDao.delete(id);
		userAccountDao.delete(id);

	}

	@Override
	public void deleteAll() {
		shoppingCartDao.deleteAll();
		userAccountDao.deleteAll();
	}
	
	@Override
	public ShoppingCart getByUser(UserAccount user){
		return shoppingCartDao.getByUser(user);
	}
}
